﻿package com.findMoneyGame.pages.achievement {
	import flash.events.Event;
	import flash.display.MovieClip;

	import com.findMoneyGame.events.AchievementEvent;
	import com.findMoneyGame.models.Achievement;
	import com.findMoneyGame.models.User;
	import com.findMoneyGame.models.Model;
	
	public class AchievementController extends MovieClip {
		public static var ACHIEVED = 'achieved';

		private var achievement : Achievement;
		
		private function get user () : User { return User.getInst(); }
		
		public function AchievementController(a : Achievement) {
			this.achievement = a;
			this.user.addEventListener(Model.UPDATE, userUpdate);
		}
		
		private function userUpdate(e : Event) {
			if (achievement.achieved(user)) {
				user.addMoney(achievement.reward);
				dispatchEvent(new AchievementEvent(ACHIEVED, achievement));
			}
		}
	}
	
}
